/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package controller;

import dnbW.dbW.DatabaseInfo;
import dnbW.uiW.DNBW;
import dnbW.uiW.dbW.DatabaseController;
import dnbW.uiW.dbW.DiskStorageManager;
import dnbW.dbW.DatabaseList;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletContext;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;
import java.util.ArrayList;

/**
 *
 * @author Andree
 */
public class StartUp extends HttpServlet {
    public static String DB_LIST_PATH = null;
    public static String SUXCENT_SCRIPT_PATH = null;
    
    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        response.setContentType("text/html;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {
                DB_LIST_PATH = getServletContext().getRealPath("databaseInfo/dblist.txt");
                
                SUXCENT_SCRIPT_PATH = getServletContext().getRealPath("script/SUCXENT-Colin.sql");
                
                new DatabaseList();
                            
                DNBW dnbw = new DNBW();
                dnbw.loadDatabase();
                DiskStorageManager.dbNames.add(0, "No Database Selected@localhost");
                
                request.setAttribute("DBLIST", DiskStorageManager.dbNames);
                
                ServletContext context= getServletContext();
                RequestDispatcher rd= context.getRequestDispatcher("/jsp/home.jsp");
                rd.forward(request, response);
                        
        } finally {            
            out.close();
        }
    }

    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    @Override
    public String getServletInfo() {
        return "Short description";
    }
}
